package com.topscore.uke.ui.activities;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.topscore.uke.R;
import com.topscore.uke.main.ATManager;
import com.topscore.uke.mvp.contractors.IndexPageFragmentContractor;
import com.topscore.uke.mvp.contractors.UpdateUserInfoContractor;
import com.topscore.uke.mvp.presenters.IndexPageFragmentPresenter;
import com.topscore.uke.mvp.presenters.UpdateUserInfoPresenter;
import com.topscore.uke.ui.BaseCompatActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainCompatActivity extends BaseCompatActivity implements IndexPageFragmentContractor.View, View.OnClickListener {

    @Bind(R.id.fragment_container)
    FrameLayout fragmentContainer;
    @Bind(R.id.icon_home_page)
    ImageView iconHomePage;
    @Bind(R.id.icon_commodity)
    ImageView iconCommodity;
    @Bind(R.id.icon_activity)
    ImageView iconActivity;
    @Bind(R.id.icon_me)
    ImageView iconMe;
    @Bind(R.id.bottom_bar)
    LinearLayout bottomBar;
    @Bind(R.id.bottom_layout)
    LinearLayout bottomLayout;
    @Bind(R.id.title_home)
    TextView titleHome;
    @Bind(R.id.title_commodity)
    TextView titleCommodity;
    @Bind(R.id.title_activities)
    TextView titleActivities;
    @Bind(R.id.title_mine)
    TextView titleMine;
    @Bind(R.id.tab_home)
    RelativeLayout tabHome;
    @Bind(R.id.tab_commodity)
    RelativeLayout tabCommodity;
    @Bind(R.id.tab_activities)
    RelativeLayout tabActivities;
    @Bind(R.id.tab_mine)
    RelativeLayout tabMine;

    private IndexPageFragmentPresenter mPageFragmentPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ATManager.add(this);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mPageFragmentPresenter = new IndexPageFragmentPresenter(this, getSupportFragmentManager(), fragmentContainer.getId());
//        mUpdateUserInfoPresenter = new UpdateUserInfoPresenter(this);
        iniViews();
    }

    private void iniViews() {
        tabHome.setOnClickListener(this);
        tabCommodity.setOnClickListener(this);
        tabActivities.setOnClickListener(this);
        tabMine.setOnClickListener(this);
//        mUpdateUserInfoPresenter.updateUserInfo();
        mPageFragmentPresenter.onHomePage();
    }

    @Override
    public void onClearSelected() {
        iconHomePage.setImageResource(R.mipmap.homepage_default);
        titleHome.setTextColor(getResources().getColor(R.color.page_title_default));
        iconCommodity.setImageResource(R.mipmap.commodity_default);
        titleCommodity.setTextColor(getResources().getColor(R.color.page_title_default));
        iconActivity.setImageResource(R.mipmap.activity_default);
        titleActivities.setTextColor(getResources().getColor(R.color.page_title_default));
        iconMe.setImageResource(R.mipmap.me_default);
        titleMine.setTextColor(getResources().getColor(R.color.page_title_default));
    }

    @Override
    public void onSelectHome() {
        iconHomePage.setImageResource(R.mipmap.homepage_selected);
        titleHome.setTextColor(getResources().getColor(R.color.page_title_selected));
    }

    @Override
    public void onSelectCommodity() {
        iconCommodity.setImageResource(R.mipmap.commodity_selected);
        titleCommodity.setTextColor(getResources().getColor(R.color.page_title_selected));
    }

    @Override
    public void onSelectActivities() {
        iconActivity.setImageResource(R.mipmap.activity_selected);
        titleActivities.setTextColor(getResources().getColor(R.color.page_title_selected));
    }

    @Override
    public void onSelectMine() {
        iconMe.setImageResource(R.mipmap.me_selected);
        titleMine.setTextColor(getResources().getColor(R.color.page_title_selected));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tab_home:
                mPageFragmentPresenter.onHomePage();
                break;
            case R.id.tab_commodity:
                mPageFragmentPresenter.onCommodity();
                break;
            case R.id.tab_activities:
                mPageFragmentPresenter.onActivities();
                break;
            case R.id.tab_mine:
                mPageFragmentPresenter.onMine();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ATManager.backExit(mContext);
        }
        return false;
    }

//    @Override
//    public void onLoading(String loadingMessage) {
//
//    }
//
//    @Override
//    public void onLoadFinished() {
//
//    }
//
//    @Override
//    public void onFail(int code, String errorMessage) {
//        Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
//    }
}
